﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using fetco.entities;

namespace fetco.data
{
    public class WebPortalDAO
    {
        private Connector _conn;
        private static WebPortalDAO _instance;

        public WebPortalDAO()
        {
            this._conn = Connector.GetInstance();
        }
        public static WebPortalDAO GetInstance()
        {
            if (_instance == null)
            {
                _instance = new WebPortalDAO();
            }
            return _instance;
        }

        #region Create WebPortal
        private WebPortal CreateWebPortal(SqlDataReader reader)
        {
            WebPortal webportal = new WebPortal();

            if (!reader.IsDBNull(reader.GetOrdinal(WebPortalField.WebId.ToString())))
                webportal.WebId = (int)reader[WebPortalField.WebId.ToString()];

            if (!reader.IsDBNull(reader.GetOrdinal(WebPortalField.WebName.ToString())))
                webportal.WebName = (string)reader[WebPortalField.WebName.ToString()];

            if (!reader.IsDBNull(reader.GetOrdinal(WebPortalField.WebLink.ToString())))
                webportal.WebLink = (string)reader[WebPortalField.WebLink.ToString()];

            if (!reader.IsDBNull(reader.GetOrdinal(WebPortalField.WebKey.ToString())))
                webportal.WebKey = (string)reader[WebPortalField.WebKey.ToString()];

            if (!reader.IsDBNull(reader.GetOrdinal(WebPortalField.WebPublished.ToString())))
                webportal.WebPublished = (int)reader[WebPortalField.WebPublished.ToString()];

            if (!reader.IsDBNull(reader.GetOrdinal(WebPortalField.WebStatus.ToString())))
                webportal.WebStatus = (int)reader[WebPortalField.WebStatus.ToString()];

            if (!reader.IsDBNull(reader.GetOrdinal(WebPortalField.WebOrdering.ToString())))
                webportal.WebOrdering = (int)reader[WebPortalField.WebOrdering.ToString()];

            return webportal;
        }
        private WebPortal CreateWebPortal(SqlDataReader reader, int numfield)
        {
            WebPortal webportal = new WebPortal();
            for (int index = 0; index < numfield; index++)
            {
                if (!reader.IsDBNull(index))
                {
                    switch (reader.GetName(index))
                    {
                        case "WebId":
                            {
                                webportal.WebId = (int)reader[index];
                                break;
                            }
                        case "WebName":
                            {
                                webportal.WebName = (string)reader[index];
                                break;
                            }
                        case "WebLink":
                            {
                                webportal.WebLink = (string)reader[index];
                                break;
                            }
                        case "WebKey":
                            {
                                webportal.WebKey = (string)reader[index];
                                break;
                            }
                        case "WebPublished":
                            {
                                webportal.WebPublished = (int)reader[index];
                                break;
                            }
                        case "WebStatus":
                            {
                                webportal.WebStatus = (int)reader[index];
                                break;
                            }
                        case "WebOrdering":
                            {
                                webportal.WebOrdering = (int)reader[index];
                                break;
                            }
                        default:
                            {
                                break;
                            }
                    }
                }
            }
            return webportal;
        }
        #endregion

        #region Get Data
        public WebPortal GetWebPortalById(int webportal_id)
        {
            WebPortal webportal = null;
            string output = WebPortalField.WebId.ToString()+","+
                            WebPortalField.WebName.ToString()+","+
                            WebPortalField.WebLink.ToString() + "," +
                            WebPortalField.WebKey.ToString() + "," +
                            WebPortalField.WebPublished.ToString() + "," +
                            WebPortalField.WebStatus.ToString() + "," +
                            WebPortalField.WebOrdering.ToString();
            string condition = WebPortalField.WebId.ToString() + "=" + webportal_id.ToString();
            string query = @"select " + output + " from " + WebPortal.TableName + " where " + condition;
            SqlDataReader reader = this._conn.ExcuteReader(query);
            if (reader != null && reader.Read())
            {
                webportal = this.CreateWebPortal(reader);
                reader.Close();
            }
            return webportal;
        }
        public ListWebPortal GetListWebPortal(OrderByDirection order)
        {
            ListWebPortal listwebportal = new ListWebPortal();
            string output = WebPortalField.WebId.ToString() + "," +
                            WebPortalField.WebName.ToString() + "," +
                            WebPortalField.WebLink.ToString() + "," +
                            WebPortalField.WebKey.ToString() + "," +
                            WebPortalField.WebPublished.ToString() + "," +
                            WebPortalField.WebStatus.ToString() + "," +
                            WebPortalField.WebOrdering.ToString();
            string ordering = WebPortalField.WebOrdering.ToString() + " " + order.ToString();
            string query = @"select " + output + " from " + WebPortal.TableName + " order by "+ordering;
            SqlDataReader reader = this._conn.ExcuteReader(query);
            if (reader != null )
            {
                while (reader.Read())
                {
                    WebPortal webportal = this.CreateWebPortal(reader);
                    listwebportal.Add(webportal);
                }
                reader.Close();
            }
            return listwebportal;
        }
        public ListWebPortal GetListWebPortal(string fieldname,string condition, OrderByDirection order)
        {
            ListWebPortal listwebportal = new ListWebPortal();
            string ordering = WebPortalField.WebOrdering.ToString() + " " + order.ToString();
            string query = @"select " + fieldname + " from " + WebPortal.TableName + " where " + condition + " order by " + ordering;
            SqlDataReader reader = this._conn.ExcuteReader(query);
            if (reader != null)
            {
                while (reader.Read())
                {
                    WebPortal webportal = this.CreateWebPortal(reader);
                    listwebportal.Add(webportal);
                }
                reader.Close();
            }
            return listwebportal;
        }
        #endregion

        #region Set Data
        public void SetWebPortal(WebPortal webportal)
        {
            string input =  WebPortalField.WebName.ToString() + "," +
                            WebPortalField.WebLink.ToString() + "," +
                            WebPortalField.WebKey.ToString() + "," +
                            WebPortalField.WebPublished.ToString() + "," +
                            WebPortalField.WebStatus.ToString() + "," +
                            WebPortalField.WebOrdering.ToString();
            string value = "'" + webportal.WebName + "'," +
                           "'" + webportal.WebLink + "'," +
                           "'" + webportal.WebKey + "'," +
                                 webportal.WebPublished + "," +
                                 webportal.WebStatus + "," +
                                 webportal.WebOrdering;

            string query = @"insert into " + WebPortal.TableName + "(" + input + ") values (" + value + ")";
            if (this._conn.ExcuteNonQuery(query) > 0)
            {
                webportal.WebId = (int)this._conn.GetMaxValue(WebPortalField.WebId.ToString(),WebPortal.TableName);
            }
        }
        #endregion

        #region Update Data
        public bool UpdateWebPortal(WebPortal webportal)
        {
            string value =  WebPortalField.WebName.ToString() + "='" + webportal.WebName + "'," +
                            WebPortalField.WebLink.ToString() + "='" + webportal.WebLink + "'," +
                            WebPortalField.WebKey.ToString() + "='" + webportal.WebKey + "'," +
                            WebPortalField.WebPublished.ToString() + "=" + webportal.WebPublished + "," +
                            WebPortalField.WebStatus.ToString() + "=" + webportal.WebStatus + "," +
                            WebPortalField.WebOrdering.ToString()+ "=" +webportal.WebOrdering;
            string condition = WebPortalField.WebId.ToString() + "=" + webportal.WebId;
            string query = @"update " + WebPortal.TableName + " set " + value + " where " + condition;
            if (this._conn.ExcuteNonQuery(query) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region Delete Data
        public bool DelWebPortal(int webportal_id)
        {
            string condition = WebPortalField.WebId.ToString() + "=" + webportal_id;
            string query = @"delete from " + WebPortal.TableName + " where " + condition;
            if (this._conn.ExcuteNonQuery(query) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

    }
}
